﻿using Corina.Datamodel.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Corina.Utilities
{
    public static class DataUtility
    {
        public static List<T> ToList<T>(this DataTable data) where T : DatamodelEntityBase, new()
        {
            List<T> entitys = new List<T>();
            if (data != null)
            {
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    T entity = new T();
                    for (int j = 0; j < data.Columns.Count; j++)
                    {
                        string property_name = data.Columns[j].ColumnName;
                        if (entity.ExistProperty(property_name))
                        {
                            entity.SetPropertyValue(property_name, data.Rows[i][property_name]);
                        }
                    }
                    entitys.Add(entity);
                }
            }
            return entitys;
        }
    }
}
